package com.changgou.business.service.impl;

import com.changgou.business.dao.AdMapper;
import com.changgou.business.pojo.Ad;
import com.changgou.business.service.Adservice;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class AdServiceImpl implements Adservice {
  // 注入mapper
  @Autowired
 private  AdMapper adMapper;
  /**
   * 查询所有
   *
   * @return
   */
  @Override
  public List<Ad> findAll() {
    return adMapper.selectAll();
  }

  /**
   * 根据id查询
   *
   * @param id
   * @return
   */
  @Override
  public Ad findAdById(Integer id) {
    return adMapper.selectByPrimaryKey(id);
  }

  /**
   * 新增
   *
   * @param ad
   */
  @Override
  public void add(Ad ad) {
    adMapper.insert(ad);
  }

  /**
   * 修改
   *
   * @param ad
   */
  @Override
  public void update(Ad ad) {
    adMapper.updateByPrimaryKey(ad);
  }

  /**
   * 根据id删除
   *
   * @param id
   */
  @Override
  public void delete(Integer id) {
    adMapper.deleteByExample(id);
  }

  /**
   * 多条件查询
   *
   * @param searchMap
   * @return
   */
  @Override
  public List<Ad> findList(Map<String, Object> searchMap) {
    Example example = createExample(searchMap);

    return adMapper.selectByExample(example);
  }

  /**
   * 分页查询
   *
   * @param page
   * @param size
   * @return
   */
  @Override
  public Page<Ad> findPage(int page, int size) {
    PageHelper.startPage(page,size);
      PageInfo
    return (Page<Ad>) adMapper.selectAll();
  }

  /**
   * 多条件分页查询
   *
   * @param searchMap
   * @param page
   * @param size
   * @return
   */
  @Override
  public Page<Ad> findPage(Map<String, Object> searchMap, int page, int size) {
    Example example = createExample(searchMap);
    PageHelper.startPage(page,size);
    return (Page<Ad>) adMapper.selectByExample(example);
  }

  /**
   * 多条件查询构建查询对象
   *
   * @param searchMap
   * @return
   */
  private Example createExample(Map<String, Object> searchMap) {
    Example example = new Example(Ad.class);
    Example.Criteria criteria = example.createCriteria();
    if (searchMap != null) {
      // 广告名称
      if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
        criteria.andLike("name", "%" + searchMap.get("name") + "%");
      }
      // 广告位置
      if (searchMap.get("position") != null && !"".equals(searchMap.get("position"))) {
        criteria.andLike("position", "%" + searchMap.get("position") + "%");
      }
      // 状态
      if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
        criteria.andEqualTo("status", searchMap.get("status"));
      }
      // 图片地址
      if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
        criteria.andLike("image", "%" + searchMap.get("image") + "%");
      }
      // URL
      if (searchMap.get("url") != null && !"".equals(searchMap.get("url"))) {
        criteria.andLike("url", "%" + searchMap.get("url") + "%");
      }
      // 备注
      if (searchMap.get("remarks") != null && !"".equals(searchMap.get("remarks"))) {
        criteria.andLike("remarks", "%" + searchMap.get("remarks") + "%");
      }

      // ID
      if (searchMap.get("id") != null) {
        criteria.andEqualTo("id", searchMap.get("id"));
      }
    }
    return example;
  }
}
